package cn.gailvlun.gll.presentation.emotion.widget;

import android.content.Context;
import android.databinding.BindingAdapter;
import android.databinding.DataBindingUtil;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.GridView;

import java.util.ArrayList;
import java.util.List;

import cn.gailvlun.gll.R;
import cn.gailvlun.gll.databinding.ItemEmotionLibHeaderTabBinding;
import cn.gailvlun.gll.net.emotion.EmotionEnum;



public class TabGridView extends GridView {


    private TabAdapter mAdapter;
    private TabGridView mTabGridView;
    private Integer mCurCheckedPosition;


    public TabGridView(Context context) {
        super(context);
        init();
    }

    public TabGridView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public TabGridView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }


    public void init() {
        mTabGridView = this;
        mAdapter = new TabAdapter(getContext());
        setAdapter(mAdapter);


    }

    public void setItem(List<String> tabList) {
        mAdapter.setAll(tabList);
    }

    @BindingAdapter("setItem")
    public static void setItem(TabGridView tabGridView, List<String> tabList) {
        if (tabGridView != null) {
            tabGridView.setItem(tabList);
        }
    }


    class TabAdapter extends BaseAdapter {
        private List<String> mTabLis = new ArrayList<>();
        private ItemEmotionLibHeaderTabBinding mBinding;
        private LayoutInflater mInflater;

        public TabAdapter(Context context) {
            mInflater = LayoutInflater.from(context);
        }

        @Override
        public int getCount() {
            return mTabLis.size();
        }

        @Override
        public String getItem(int i) {
            return mTabLis.get(i);
        }

        @Override
        public long getItemId(int i) {
            return 0;
        }

        @Override
        public View getView(final int position, View view, ViewGroup viewGroup) {
            mBinding = DataBindingUtil.inflate(mInflater, R.layout.item_emotion_lib_header_tab, viewGroup, false);
            if (mCurCheckedPosition == null && getItem(position).equals("全部")) {
                mBinding.btContent.setChecked(true);
            } else if (mCurCheckedPosition !=null && mCurCheckedPosition == position) {
                mBinding.btContent.setChecked(true);
            } else {
                mBinding.btContent.setChecked(false);
            }
            mBinding.btContent.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View view) {

                    for (int i = 0; i < mTabGridView.getCount(); i++) {

                        CheckBox childAt = (CheckBox) mTabGridView.getChildAt(i);
                        if (position == i) {
                            childAt.setChecked(true);
                            mCurCheckedPosition=position;
                        } else {
                            childAt.setChecked(false);
                        }

                    }

                    EmotionEnum myState = null;
                    for (EmotionEnum state :
                            EmotionEnum.values()) {
                        if (state.getDesc().equals(mAdapter.getTabLis().get(position))) {
                            myState = state;
                            break;
                        }
                    }
                    if (mOnCheckedChangeListener != null) {
                        mOnCheckedChangeListener.onCheckedChanged(mBinding.btContent, myState, position);
                    }
                }
            });
            mBinding.setTab(getItem(position));
            return mBinding.getRoot();
        }

        public void setAll(List<String> tabList) {
            if (tabList != null) {
                mTabLis.clear();
                mTabLis.addAll(tabList);
                notifyDataSetChanged();
            }

        }


        public List<String> getTabLis() {
            return mTabLis;
        }

    }

    private OnCheckedChangeListener mOnCheckedChangeListener;

    public interface OnCheckedChangeListener {
        void onCheckedChanged(View view, EmotionEnum state, int position);
    }

    public void setOnCheckedChangeListener(OnCheckedChangeListener mOnCheckedChangeListener) {
        this.mOnCheckedChangeListener = mOnCheckedChangeListener;
    }

    public Integer getCurCheckedPosition() {
        return mCurCheckedPosition;
    }
}
